package ctrservice

import (
    "io/ioutil"
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego/logs"
    "cloud_worm/wormweb/controllers/ctrconfig"
    "cloud_worm/util/uhttp"
    "cloud_worm/util/proto/urpc"
    "cloud_worm/util/umodels/mysqldb"
    "cloud_worm/wormweb/controllers/ctrutil"
)

// @Description: 用于查询实时online在线的
type AgentRealOnline struct {
    ctrconfig.LoginCtr
}

// @Description：基于传入的server http，查询所有实时在线的
func (w *AgentRealOnline) Get()  {
    w.Admin()

    accessSrv := w.GetString("access_srv", mysqldb.SelectStrAll)
    if accessSrv == mysqldb.SelectStrAll {
        w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input access srv error.", nil)
        w.ServeJSON()
        return
    }

    args := urpc.AllOnlineArgs{}
    reply := urpc.AllOnlineReply{}
    err := ctrutil.AccessSrvRpc.Call(accessSrv, "StatusAbout.GetAllOnline", &args, &reply)
    if err != nil {
        logs.Error(fmt.Sprintf("rpc call error: %s", err.Error()))
        w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "rpc call error", nil)
        w.ServeJSON()
        return
    }

    w.Data["json"] = uhttp.CreateOkResponse(reply.Hosts)
    w.ServeJSON()
    return
}

type AgentUpdateController struct {
    ctrconfig.LoginCtr
}

func (w *AgentUpdateController) Update() {
    w.Admin()

    type UpdateInfo struct {
        OldVersion      int
        NewVersion      int
        AccessSrv       string
    }
    body, err := ioutil.ReadAll(w.Ctx.Request.Body)
    if err != nil {
        logs.Error(fmt.Sprintf("read body error: %s", err.Error()))
        w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, err.Error(), nil)
        w.ServeJSON()
        return
    }
    var input UpdateInfo
    if err = json.Unmarshal(body, &input); err != nil {
        logs.Error(fmt.Sprintf("Unmarshal input error: %s", err.Error()))
        w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeReqBodyErr, "Unmarshal input err.", nil)
        w.ServeJSON()
        return
    }
    if input.OldVersion == mysqldb.SelectIntAll || input.NewVersion == mysqldb.SelectIntAll {
        logs.Error(fmt.Sprintf("input version info error"))
        w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeParamErr, "input version error", nil)
        w.ServeJSON()
        return
    }

    // todo: 基于old version和access srv执行选择
    //worm := dbmanager.WormId{Version: oldVer}
    //wolIds, err := worm.SelectIPAndWolID(mysqldb.SelectOffsetZero, mysqldb.SelectLimitAll, dbmanager.WolAgentOnline,
    //    oldVer, mysqldb.SelectStrAll, mysqldb.SelectStrAll, mysqldb.SelectStrAll,
    //        mysqldb.SelectStrAll, mysqldb.SelectIntAll, mysqldb.SelectIntAll)
    //if err != nil {
    //    w.Data["json"] = uhttp.CreateErrResponse(uhttp.RespCodeSystemErr, "select error", err.Error())
    //    w.ServeJSON()
    //    return
    //}

    // todo: 执行update操作

    // todo: 升级失败的，利用im发送错误通知
    //if len(errs) > 0 {
    //    ttItem := &unotify.IMNotifyItem{
    //        Grade:       unotify.ErrorGrade,
    //        Title:       unotify.DefaultTitle,
    //        Msg:         fmt.Sprintf("post wukong failed: %s", strings.Join(errs, "|")),
    //        IMUsers: []string{
    //            "用学",
    //        },
    //    }
    //    unotify.IMNotifyInst().SendAsync(ttItem)
    //}

    w.Data["json"] = uhttp.CreateOkResponse(nil)
    w.ServeJSON()
    return
}